package com.ceba.test.modules.regist.utils;

import com.ceba.base.config.CommonConfig;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.test.base.enums.BeKyeEnum;
import com.ceba.test.base.utils.StringUtil;
import com.ceba.test.modules.regist.entity.EnterpriseRegistLic;
import org.json.JSONArray;
import org.json.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class LicJsonUtils {


    /**
     * ATM 所需的json格式
     *[{"KEY":"SETUP_TIME","VALUE":"2018-04-04 04:59:06"},
     {"KEY":"SETUP_PATH_PROGRAM","VALUE":"D:\\\\ceba\\\\fs"},
     {"KEY":"SETUP_PATH_MYSQL","VALUE":"D:\\\\ceba\\\\db"},
     {"KEY":"NETTY_SERVER_IP","VALUE":"127.0.0.1"}]
     *
     */

    /**
     * 将ATM传入的json转换成lic对象
     */

    public static EnterpriseRegistLic jsonConvertObject(String strJson) throws  Exception{
        EnterpriseRegistLic enterpriseRegistLic = new EnterpriseRegistLic();
        JSONArray arr = new JSONArray(strJson);
        for (int i = 0; i < arr.length(); i++) {
            JSONObject oj = (JSONObject) arr.get(i);
            String KEY = oj.getString("KEY");
            String VALUE = oj.getString("VALUE");
            //将值设置到对象  对应的属性当中
            keyConvertValue(oj,enterpriseRegistLic);
        }
        return  enterpriseRegistLic;

    }


    /**
     * 将对象转换成ATM所需的json格式
     */

    public static  String objectConvertJson(EnterpriseRegistLic lic) throws Exception{
        JSONArray arr = new JSONArray();
        if(!StringUtil.isEmptyString(lic.getAllowLoginDate())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","ALLOW_LOGIN_DATE");
            oj.put("VALUE",CommonConfig.getEncode(lic.getAllowLoginDate().replace("/","-")));
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getAllowSignNum())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","ALLOW_SIGN_NUM");
            int value = Integer.valueOf(lic.getAllowSignNum());
//            oj.put("VALUE",CommonConfig.getEncode(lic.getAllowSignNum()));
            //剩余份数归零，直接给一个这次增加的份数过去
            oj.put("VALUE",CommonConfig.getEncode(value+""));
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(CommonConfig.getEncode(lic.getAllSignNum()))){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","ALL_SIGN_NUM");
            oj.put("VALUE",CommonConfig.getEncode(lic.getAllSignNum()));
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getEnterpriseName())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SIGN_COMPANY");
            oj.put("VALUE",CommonConfig.getEncode(lic.getEnterpriseName()));
            arr.put(oj);
        }
        //如果有公司名称，则默认为激活
        if(!StringUtil.isEmptyString(lic.getEnterpriseName())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SING_COMPANY_ACTIVATE_FLAG");
            oj.put("VALUE","true");
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSignedCount())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SIGNED_COUNT");
            oj.put("VALUE",CommonConfig.getEncode(lic.getSignedCount()));
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getRemoteNewSignCount())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","REMOTE_NEW_SIGN_COUNT");
            oj.put("VALUE",CommonConfig.getEncode(lic.getRemoteNewSignCount()));
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getActiveTimes())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","ACTIVE_TIMES");
            oj.put("VALUE",lic.getActiveTimes());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getOnlineWorker())){
            JSONObject oj  = new JSONObject();
            BASE64Encoder encoder = new BASE64Encoder();
            oj.put("KEY","ONLINE_WORKER");
            oj.put("VALUE",encoder.encode(lic.getOnlineWorker().getBytes("utf-8")));
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSignValidity())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SIGN_VALIDITY");
            oj.put("VALUE",lic.getSignValidity().replace("/","-"));
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getFormalUseTime())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","FORMAL_USE_TIME");
            oj.put("VALUE",lic.getFormalUseTime());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSystemType())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SYSTEM_TYPE");
            oj.put("VALUE",CommonConfig.getEncode(lic.getSystemType()));
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSystemFeeType())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SYSTEM_FEE_TYPE");
            oj.put("VALUE",lic.getSystemFeeType());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getUseVersion())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","USE_VERSION");
            oj.put("VALUE",lic.getUseVersion());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getCustomerGroup())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","CUSTOMER_GROUP");
            oj.put("VALUE",lic.getCustomerGroup());
            arr.put(oj);
        }

        if(!StringUtil.isEmptyString(lic.getShcaKeyFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SHCA_KEY_FLAG");
            oj.put("VALUE",lic.getShcaKeyFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSvnVersion())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SVN_VERSION");
            oj.put("VALUE",lic.getSvnVersion());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getVersion())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","VERSION");
            oj.put("VALUE",lic.getVersion());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSmtSystemDefaultsLanguage())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SMT_SYSTEM_DEFAULTS_LANGUAGE");
            oj.put("VALUE",lic.getSmtSystemDefaultsLanguage());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSystemDefaultsLanguage())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SYSTEM_DEFAULTS_LANGUAGE");
            oj.put("VALUE",lic.getSystemDefaultsLanguage());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSystemProductName())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SYSTEM_PRODUCT_NAME");
            oj.put("VALUE",lic.getSystemProductName());
            arr.put(oj);
        }else if(!StringUtil.isEmptyString(lic.getIdsServerPort())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","IDS_SERVER_PORT");
            oj.put("VALUE",lic.getIdsServerPort());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getNettyServerIp())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","NETTY_SERVER_IP");
            oj.put("VALUE",lic.getNettyServerIp());
            arr.put(oj);
        }
        if (!StringUtil.isEmptyString(lic.getNettyServerPort())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","NETTY_SERVER_PORT");
            oj.put("VALUE",lic.getNettyServerPort());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getWatermarkSvercontentFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","WATERMARK_OVERCONTENT_FLAG");
            oj.put("VALUE",lic.getWatermarkSvercontentFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getLoginType())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","LOGIN_TYPE");
            oj.put("VALUE",lic.getLoginType());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getFileStorageFalg())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","FILE_STORAGE_FALG");
            oj.put("VALUE",lic.getFileStorageFalg());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getPdfCreateMode())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","PDF_CREATE_MODE");
            oj.put("VALUE",lic.getPdfCreateMode());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getPatterLogoFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","PATTER_LOGO_FLAG");
            oj.put("VALUE",lic.getPatterLogoFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getZipDownMaxSize())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","ZIP_DOWN_MAX_SIZE");
            oj.put("VALUE",lic.getZipDownMaxSize());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getMaxSize())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","MAX_SIZE");
            oj.put("VALUE",lic.getMaxSize());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getPdfTransformUrl())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","PDF_TRANSFORM_URL");
            oj.put("VALUE",lic.getPdfTransformUrl());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getPdfTransformSocketPort())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","PDF_TRANSFORM_SOCKET_PORT");
            oj.put("VALUE",lic.getPdfTransformSocketPort());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getGroupManagerFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","GROUP_MANAGER_FLAG");
            oj.put("VALUE",lic.getGroupManagerFlag());
            arr.put(oj);
        }
        if (!StringUtil.isEmptyString(lic.getSeGroupFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SE_GROUP_FLAG");
            oj.put("VALUE",lic.getSeGroupFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSeDeleteSingleFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SE_DELETE_SINGLE_FLAG");
            oj.put("VALUE",lic.getSeDeleteSingleFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getMaxPage())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","MAX_PAGE");
            oj.put("VALUE",lic.getMaxPage());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getPublicKeyFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","PUBLIC_KEY_FLAG");
            oj.put("VALUE",lic.getPublicKeyFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getBatchSign())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","BATCH_SIGN");
            oj.put("VALUE",lic.getBatchSign());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSendMailMaxSize())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SEND_MAIL_MAX_SIZE");
            oj.put("VALUE",lic.getSendMailMaxSize());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getPcCodes())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","PC_CODES");
            oj.put("VALUE",lic.getPcCodes());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getMergeMaxCount())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","MERGE_MAX_COUNT");
            oj.put("VALUE",lic.getMergeMaxCount());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getQifengFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","QIFENG_FLAG");
            oj.put("VALUE",lic.getQifengFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getReportmergeFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","REPORTMERGE_FLAG");
            oj.put("VALUE",lic.getReportmergeFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getSignFileMax())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SIGN_FILE_MAX");
            oj.put("VALUE",lic.getSignFileMax());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getBeKeyFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","BE_KEY_FLAG");
            oj.put("VALUE",lic.getBeKeyFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getShowDirManage())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SHOW_DIR_MANAGE");
            oj.put("VALUE",lic.getShowDirManage());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getLicenseImportTime())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","LICENSE_IMPORT_TIME");
            oj.put("VALUE",lic.getLicenseImportTime());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getHiddenEmailFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","HIDDEN_EMAIL_FLAG");
            oj.put("VALUE",lic.getHiddenEmailFlag());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getQrCode())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","QRCODE");
            oj.put("VALUE",lic.getQrCode());
            arr.put(oj);
        }
        if(!StringUtil.isEmptyString(lic.getServerSignFlow())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","SERVER_SIGN_FLOW");
            oj.put("VALUE",lic.getServerSignFlow());
            arr.put(oj);
        }

        if(!StringUtil.isEmptyString(lic.getAutomaticSignatureFlag())){
            JSONObject oj  = new JSONObject();
            oj.put("KEY","AUTOMATIC_SIGNATURE_FLAG");
            oj.put("VALUE",lic.getAutomaticSignatureFlag());
            arr.put(oj);
        }

        return  arr.toString();
    }




    /**
     *json key值 对比  重组
     *
     */

    private static void keyConvertValue(JSONObject oj,EnterpriseRegistLic enterpriseRegistLic) throws Exception{
        String KEY = oj.getString("KEY");
        String VALUE = oj.getString("VALUE");
        if(KEY.equals("FK_ENTERPISE_ID")){
            enterpriseRegistLic.setFkEnterpiseId(VALUE);
        }else if(KEY.equals("ALLOW_LOGIN_DATE")){
            enterpriseRegistLic.setAllowLoginDate(CommonConfig.getDecode(VALUE));
        }else if(KEY.equals("ALLOW_SIGN_NUM")){
            enterpriseRegistLic.setAllowSignNum(CommonConfig.getDecode(VALUE));//剩余签名份数
        }else if(KEY.equals("ALL_SIGN_NUM")){
            enterpriseRegistLic.setAllSignNum(CommonConfig.getDecode(VALUE));
        }else if(KEY.equals("SIGNED_COUNT")){
            enterpriseRegistLic.setSignedCount(CommonConfig.getDecode(VALUE));
        }else if(KEY.equals("REMOTE_NEW_SIGN_COUNT")){
            enterpriseRegistLic.setRemoteNewSignCount(CommonConfig.getDecode(VALUE));
        }else if (KEY.equals("ACTIVE_TIMES")){
            enterpriseRegistLic.setActiveTimes(VALUE);
        }else if (KEY.equals("ONLINE_WORKER")){
            BASE64Decoder decoder = new BASE64Decoder();
            enterpriseRegistLic.setOnlineWorker(new String(decoder.decodeBuffer(VALUE),"utf-8"));
        }else if (KEY.equals("SIGN_VALIDITY")){
            enterpriseRegistLic.setSignValidity(VALUE);
        }else if(KEY.equals("FORMAL_USE_TIME")){
            enterpriseRegistLic.setFormalUseTime(VALUE);
        }else if(KEY.equals("SYSTEM_TYPE")){
            enterpriseRegistLic.setSystemType(CommonConfig.getDecode(VALUE));
        }else if(KEY.equals("SYSTEM_FEE_TYPE")){
            enterpriseRegistLic.setSystemFeeType(VALUE);
        }else if(KEY.equals("USE_VERSION")){
            enterpriseRegistLic.setUseVersion(VALUE);
        }else if(KEY.equals("CUSTOMER_GROUP")){
            enterpriseRegistLic.setCustomerGroup(VALUE);
        }else if(KEY.equals("SHCA_KEY_FLAG")){
            enterpriseRegistLic.setShcaKeyFlag(VALUE);
        }else if(KEY.equals("SVN_VERSION")){
            enterpriseRegistLic.setSvnVersion(VALUE);
        }else if(KEY.equals("VERSION")){
            enterpriseRegistLic.setVersion(VALUE);
        }else if(KEY.equals("SMT_SYSTEM_DEFAULTS_LANGUAGE")){
            enterpriseRegistLic.setSmtSystemDefaultsLanguage(VALUE);
        }else if(KEY.equals("SYSTEM_DEFAULTS_LANGUAGE")){
            enterpriseRegistLic.setSystemDefaultsLanguage(VALUE);
        }else if(KEY.equals("SYSTEM_PRODUCT_NAME")){
            enterpriseRegistLic.setSystemProductName(VALUE);
        }else if(KEY.equals("IDS_SERVER_PORT")){
            enterpriseRegistLic.setIdsServerPort(VALUE);
        }else if (KEY.equals("NETTY_SERVER_IP")){
            enterpriseRegistLic.setNettyServerIp(VALUE);
        }else if(KEY.equals("NETTY_SERVER_PORT")){
            enterpriseRegistLic.setNettyServerPort(VALUE);
        }else if(KEY.equals("WATERMARK_OVERCONTENT_FLAG")){
            enterpriseRegistLic.setWatermarkSvercontentFlag(VALUE);
        }else if(KEY.equals("LOGIN_TYPE")){
            enterpriseRegistLic.setLoginType(VALUE);
        }else if(KEY.equals("FILE_STORAGE_FALG")){
            enterpriseRegistLic.setFileStorageFalg(VALUE);
        }else if(KEY.equals("PDF_CREATE_MODE")){
            enterpriseRegistLic.setPdfCreateMode(VALUE);
        }else if(KEY.equals("PATTER_LOGO_FLAG")){
            enterpriseRegistLic.setPatterLogoFlag(VALUE);
        }else if(KEY.equals("ZIP_DOWN_MAX_SIZE")){
            enterpriseRegistLic.setZipDownMaxSize(VALUE);
        }else if(KEY.equals("MAX_SIZE")){
            enterpriseRegistLic.setMaxSize(VALUE);
        }else if(KEY.equals("PDF_TRANSFORM_URL")){
            enterpriseRegistLic.setPdfTransformUrl(VALUE);
        }else if (KEY.equals("PDF_TRANSFORM_SOCKET_PORT")){
            enterpriseRegistLic.setPdfTransformSocketPort(VALUE);
        }else if(KEY.equals("GROUP_MANAGER_FLAG")){
            enterpriseRegistLic.setGroupManagerFlag(VALUE);
        }else if (KEY.equals("SE_GROUP_FLAG")){
            enterpriseRegistLic.setSeGroupFlag(VALUE);
        }else if(KEY.equals("SE_DELETE_SINGLE_FLAG")){
            enterpriseRegistLic.setSeDeleteSingleFlag(VALUE);
        }else if (KEY.equals("MAX_PAGE")){
            enterpriseRegistLic.setMaxPage(VALUE);
        }else if(KEY.equals("PUBLIC_KEY_FLAG")){
            enterpriseRegistLic.setPublicKeyFlag(VALUE);
        }else if(KEY.equals("BATCH_SIGN")){
            enterpriseRegistLic.setBatchSign(VALUE);
        }else if(KEY.equals("SEND_MAIL_MAX_SIZE")){
            enterpriseRegistLic.setSendMailMaxSize(VALUE);
        }else if (KEY.equals("MERGE_MAX_COUNT")){
            enterpriseRegistLic.setMergeMaxCount(VALUE);
        }else if(KEY.equals("QIFENG_FLAG")){
            enterpriseRegistLic.setQifengFlag(VALUE);
        }else if(KEY.equals("REPORTMERGE_FLAG")){
            enterpriseRegistLic.setReportmergeFlag(VALUE);
        }else if(KEY.equals("SIGN_FILE_MAX")){
            enterpriseRegistLic.setSignFileMax(VALUE);
        }else if(KEY.equals("BE_KEY_FLAG")){
            enterpriseRegistLic.setBeKeyFlag(VALUE);
        }else if(KEY.equals("SHOW_DIR_MANAGE")){
            enterpriseRegistLic.setShowDirManage(VALUE);
        }else if(KEY.equals("PC_CODES")){
            enterpriseRegistLic.setPcCodes(VALUE);
        }else if(KEY.equals("LICENSE_IMPORT_TIME")){
            enterpriseRegistLic.setLicenseImportTime(VALUE);
        }else if(KEY.equals("HIDDEN_EMAIL_FLAG")){
            enterpriseRegistLic.setHiddenEmailFlag(VALUE);
        }else if(KEY.equals("QRCODE")){
            enterpriseRegistLic.setQrCode(VALUE);
        }else if(KEY.equals("SERVER_SIGN_FLOW")){
            enterpriseRegistLic.setServerSignFlow(VALUE);
        }else if(KEY.equals("AUTOMATIC_SIGNATURE_FLAG")){
            enterpriseRegistLic.setAutomaticSignatureFlag(VALUE);
        }
    }



    public static void main(String[] args) throws Exception {
        String jsonStr = "[{\"KEY\":\"SETUP_TIME\",\"VALUE\":\"2018-04-04 04:59:06\"},\n" +
                "     {\"KEY\":\"SETUP_PATH_PROGRAM\",\"VALUE\":\"D:\\\\\\\\ceba\\\\\\\\fs\"},\n" +
                "     {\"KEY\":\"SETUP_PATH_MYSQL\",\"VALUE\":\"D:\\\\\\\\ceba\\\\\\\\db\"},\n" +
                "     {\"KEY\":\"NETTY_SERVER_IP\",\"VALUE\":\"127.0.0.1\"}]";
        jsonConvertObject(jsonStr);

    }






}
